/**
 * @file app\data_proc\dp_key.c
 *
 * Copyright (C) 2023
 *
 * dp_key.c is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author zhuodada
 *
 * @encoding utf-8
 */

/*---------- includes ----------*/
#include "key.h"
#include "cpu.h"
#include "device.h"
#include "gpio_group.h"
#include "htr32xx.h"
#include "multi_button.h"
#include "options.h"
#include "protocol_mobus.h"
#include "relay.h"
#include "resources.h"
#include "soft_timer.h"
#include "storage.h"

/*---------- macro ----------*/
#define TAG "DPKey"

/*---------- type define ----------*/
/*---------- variable prototype ----------*/
/*---------- function prototype ----------*/
/*---------- variable ----------*/
static void* dev_motor_t = NULL;
static void* proc = NULL;
static void* proc_timer = NULL;
static void* dev_key = NULL;
static struct Button button[16] = { 0 };
static float real_adc = 0;
static float real_adc_old = 0;
static volatile uint16_t real_adc_group[10] = { 0 };
gpio_group_describe_t* pdesc = NULL;
static volatile struct struct_all_type* user_date_all = NULL;
static struct storage_mobus_device* dev = NULL;
static struct storage_lianpai_struct* lianpai_data = NULL;
static unsigned short flag_1S = 0;
/*---------- function ----------*/
static uint8_t read_button1_gpio() {
    uint8_t var1 = 0;
    device_ioctl(dev_key, (GROUP_INDEX1 | IOCTL_GROUP_GET), &var1);
    return var1;
}
static uint8_t read_button2_gpio() {
    uint8_t var1 = 0;
    device_ioctl(dev_key, (GROUP_INDEX2 | IOCTL_GROUP_GET), &var1);
    return var1;
}
static uint8_t read_button3_gpio() {
    uint8_t var1 = 0;
    device_ioctl(dev_key, (GROUP_INDEX3 | IOCTL_GROUP_GET), &var1);
    return var1;
}
static uint8_t read_button4_gpio() {
    uint8_t var1 = 0;
    device_ioctl(dev_key, (GROUP_INDEX4 | IOCTL_GROUP_GET), &var1);
    return var1;
}
static uint8_t read_button5_gpio() {
    uint8_t var1 = 0;
    device_ioctl(dev_key, (GROUP_INDEX5 | IOCTL_GROUP_GET), &var1);
    return var1;
}
static uint8_t read_button6_gpio() {
    uint8_t var1 = 0;
    device_ioctl(dev_key, (GROUP_INDEX6 | IOCTL_GROUP_GET), &var1);
    return var1;
}
static uint8_t read_button7_gpio() {
    uint8_t var1 = 0;
    device_ioctl(dev_key, (GROUP_INDEX7 | IOCTL_GROUP_GET), &var1);
    return var1;
}
static uint8_t read_button8_gpio() {
    uint8_t var1 = 0;
    device_ioctl(dev_key, (GROUP_INDEX8 | IOCTL_GROUP_GET), &var1);
    return var1;
}
static uint8_t read_button9_gpio() {
    uint8_t var1 = 0;
    device_ioctl(dev_key, (GROUP_INDEX9 | IOCTL_GROUP_GET), &var1);
    return var1;
}
static uint8_t read_button10_gpio() {
    uint8_t var1 = 0;
    device_ioctl(dev_key, (GROUP_INDEX10 | IOCTL_GROUP_GET), &var1);
    return var1;
}
static uint8_t read_button11_gpio() {
    uint8_t var1 = 0;
    device_ioctl(dev_key, (GROUP_INDEX11 | IOCTL_GROUP_GET), &var1);
    return var1;
}
static uint8_t read_button12_gpio() {
    uint8_t var1 = 0;
    device_ioctl(dev_key, (GROUP_INDEX12 | IOCTL_GROUP_GET), &var1);
    return var1;
}
static uint8_t read_button13_gpio() {
    uint8_t var1 = 0;
    device_ioctl(dev_key, (GROUP_INDEX13 | IOCTL_GROUP_GET), &var1);
    return var1;
}
static uint8_t read_button14_gpio() {
    uint8_t var1 = 0;
    device_ioctl(dev_key, (GROUP_INDEX14 | IOCTL_GROUP_GET), &var1);
    return var1;
}
static uint8_t read_button15_gpio() {
    uint8_t var1 = 0;
    device_ioctl(dev_key, (GROUP_INDEX15 | IOCTL_GROUP_GET), &var1);
    return var1;
}
static uint8_t read_button16_gpio() {
    uint8_t var1 = 0;
    device_ioctl(dev_key, (GROUP_INDEX16 | IOCTL_GROUP_GET), &var1);
    return var1;
}
static void correction_real_lednum(uint8_t btn_key_val) {
    if (user_date_all->part1 == type_x_part1 &&
        user_date_all->part2 == type_x_part1 &&
        user_date_all->part3 == type_x_part1 &&
        user_date_all->part4 == type_x_part1) // four type_1_4
    {
        if (btn_key_val == 0) {
            no_storage.led_temp_variable.led8_push = 1;
        }
        else if (btn_key_val == 1) {
            no_storage.led_temp_variable.led7_push = 1;
        }
        else if (btn_key_val == 2) {
            no_storage.led_temp_variable.led6_push = 1;
        }
        else if (btn_key_val == 3) {
            no_storage.led_temp_variable.led5_push = 1;
        }
        else if (btn_key_val == 4) {
            no_storage.led_temp_variable.led9_push = 1;
        }
        else if (btn_key_val == 5) {
            no_storage.led_temp_variable.led10_push = 1;
        }
        else if (btn_key_val == 6) {
            no_storage.led_temp_variable.led11_push = 1;
        }
        else if (btn_key_val == 7) {
            no_storage.led_temp_variable.led12_push = 1;
        }
        else if (btn_key_val == 8) {
            no_storage.led_temp_variable.led13_push = 1;
        }
        else if (btn_key_val == 9) {
            no_storage.led_temp_variable.led14_push = 1;
        }
        else if (btn_key_val == 10) {
            no_storage.led_temp_variable.led15_push = 1;
        }
        else if (btn_key_val == 11) {
            no_storage.led_temp_variable.led16_push = 1;
        }
        else if (btn_key_val == 12) {
            no_storage.led_temp_variable.led1_push = 1;
        }
        else if (btn_key_val == 13) {
            no_storage.led_temp_variable.led2_push = 1;
        }
        else if (btn_key_val == 14) {
            no_storage.led_temp_variable.led3_push = 1;
        }
        else if (btn_key_val == 15) {
            no_storage.led_temp_variable.led4_push = 1;
        }
    }
    else if (user_date_all->part1 == type_x_part2 &&
        user_date_all->part2 == type_x_part2 &&
        user_date_all->part3 == type_null_x_part &&
        user_date_all->part4 == type_null_x_part) // double type_1_2_y
    {
        if (btn_key_val == 0) {
            no_storage.led_temp_variable.led8_push = 1;
        }
        else if (btn_key_val == 1) {
            no_storage.led_temp_variable.led7_push = 1;
        }
        else if (btn_key_val == 2) {
            no_storage.led_temp_variable.led6_push = 1;
        }
        else if (btn_key_val == 3) {
            no_storage.led_temp_variable.led5_push = 1;
        }
        else if (btn_key_val == 4) {
            no_storage.led_temp_variable.led13_push = 1;
        }
        else if (btn_key_val == 5) {
            no_storage.led_temp_variable.led14_push = 1;
        }
        else if (btn_key_val == 6) {
            no_storage.led_temp_variable.led15_push = 1;
        }
        else if (btn_key_val == 7) {
            no_storage.led_temp_variable.led16_push = 1;
        }
    }
    else if (user_date_all->part1 == type_x_part1 &&
        user_date_all->part2 == type_x_part1 &&
        user_date_all->part3 == type_x_part3 &&
        user_date_all->part4 == type_null_x_part) // double type_1_4 and type_1_2_x
    {
        if (btn_key_val == 0) {
            no_storage.led_temp_variable.led8_push = 1;
        }
        else if (btn_key_val == 1) {
            no_storage.led_temp_variable.led7_push = 1;
        }
        else if (btn_key_val == 2) {
            no_storage.led_temp_variable.led6_push = 1;
        }
        else if (btn_key_val == 3) {
            no_storage.led_temp_variable.led5_push = 1;
        }
        else if (btn_key_val == 4) {
            no_storage.led_temp_variable.led9_push = 1;
        }
        else if (btn_key_val == 5) {
            no_storage.led_temp_variable.led10_push = 1;
        }
        else if (btn_key_val == 6) {
            no_storage.led_temp_variable.led11_push = 1;
        }
        else if (btn_key_val == 7) {
            no_storage.led_temp_variable.led12_push = 1;
        }
        else if (btn_key_val == 8) {
            no_storage.led_temp_variable.led13_push = 1;
        }
        else if (btn_key_val == 9) {
            no_storage.led_temp_variable.led14_push = 1;
        }
        else if (btn_key_val == 10) {
            no_storage.led_temp_variable.led15_push = 1;
        }
        else if (btn_key_val == 11) {
            no_storage.led_temp_variable.led16_push = 1;
        }

    }
    else if (user_date_all->part1 == type_x_part4 &&
        user_date_all->part2 == type_x_part5 &&
        user_date_all->part3 == type_null_x_part &&
        user_date_all->part4 == type_null_x_part) // type_1_2_slide and type_1_2_sw
    {

        if (btn_key_val == 0) {
            no_storage.led_temp_variable.led13_push = 1;
        }
        else if (btn_key_val == 1) {
            no_storage.led_temp_variable.led14_push = 1;
        }
    }
    else if (user_date_all->part1 == type_x_part3 &&
        user_date_all->part2 == type_null_x_part &&
        user_date_all->part3 == type_x_part3 &&
        user_date_all->part4 == type_null_x_part) // double type_1_2_x
    {
        if (btn_key_val == 0) {
            no_storage.led_temp_variable.led8_push = 1;
        }
        else if (btn_key_val == 1) {
            no_storage.led_temp_variable.led7_push = 1;
        }
        else if (btn_key_val == 2) {
            no_storage.led_temp_variable.led6_push = 1;
        }
        else if (btn_key_val == 3) {
            no_storage.led_temp_variable.led5_push = 1;
        }
        else if (btn_key_val == 4) {
            no_storage.led_temp_variable.led9_push = 1;
        }
        else if (btn_key_val == 5) {
            no_storage.led_temp_variable.led10_push = 1;
        }
        else if (btn_key_val == 6) {
            no_storage.led_temp_variable.led11_push = 1;
        }
        else if (btn_key_val == 7) {
            no_storage.led_temp_variable.led12_push = 1;
        }
    }
    else if (user_date_all->part1 == type_x_part2 &&
        user_date_all->part2 == type_x_part1 &&
        user_date_all->part3 == type_null_x_part &&
        user_date_all->part4 == type_x_part1) // type_1_2_y add double type_1_4
    {
        if (btn_key_val == 0) {
            no_storage.led_temp_variable.led8_push = 1;
        }
        else if (btn_key_val == 1) {
            no_storage.led_temp_variable.led7_push = 1;
        }
        else if (btn_key_val == 2) {
            no_storage.led_temp_variable.led6_push = 1;
        }
        else if (btn_key_val == 3) {
            no_storage.led_temp_variable.led5_push = 1;
        }
        else if (btn_key_val == 4) {
            no_storage.led_temp_variable.led13_push = 1;
        }
        else if (btn_key_val == 5) {
            no_storage.led_temp_variable.led14_push = 1;
        }
        else if (btn_key_val == 6) {
            no_storage.led_temp_variable.led15_push = 1;
        }
        else if (btn_key_val == 7) {
            no_storage.led_temp_variable.led16_push = 1;
        }
        else if (btn_key_val == 8) {
            no_storage.led_temp_variable.led1_push = 1;
        }
        else if (btn_key_val == 9) {
            no_storage.led_temp_variable.led2_push = 1;
        }
        else if (btn_key_val == 10) {
            no_storage.led_temp_variable.led3_push = 1;
        }
        else if (btn_key_val == 11) {
            no_storage.led_temp_variable.led4_push = 1;
        }
    }
    else if (user_date_all->part1 == type_x_part1 &&
        user_date_all->part2 == type_x_part2 &&
        user_date_all->part3 == type_x_part1 &&
        user_date_all->part4 == type_null_x_part) // double type_1_4 add  type_1_2_y
    {
        if (btn_key_val == 0) {
            no_storage.led_temp_variable.led8_push = 1;
        }
        else if (btn_key_val == 1) {
            no_storage.led_temp_variable.led7_push = 1;
        }
        else if (btn_key_val == 2) {
            no_storage.led_temp_variable.led6_push = 1;
        }
        else if (btn_key_val == 3) {
            no_storage.led_temp_variable.led5_push = 1;
        }
        else if (btn_key_val == 4) {
            no_storage.led_temp_variable.led9_push = 1;
        }
        else if (btn_key_val == 5) {
            no_storage.led_temp_variable.led10_push = 1;
        }
        else if (btn_key_val == 6) {
            no_storage.led_temp_variable.led11_push = 1;
        }
        else if (btn_key_val == 7) {
            no_storage.led_temp_variable.led12_push = 1;
        }
        else if (btn_key_val == 8) {
            no_storage.led_temp_variable.led13_push = 1;
        }
        else if (btn_key_val == 9) {
            no_storage.led_temp_variable.led14_push = 1;
        }
        else if (btn_key_val == 10) {
            no_storage.led_temp_variable.led15_push = 1;
        }
        else if (btn_key_val == 11) {
            no_storage.led_temp_variable.led16_push = 1;
        }
    }
    else if (user_date_all->part1 == type_x_part4 &&
        user_date_all->part2 == type_x_part2 &&
        user_date_all->part3 == type_null_x_part &&
        user_date_all->part4 == type_null_x_part) // type_1_2_slide and type_1_2_y
    {
        if (btn_key_val == 0) {
            no_storage.led_temp_variable.led13_push = 1;
        }
        else if (btn_key_val == 1) {
            no_storage.led_temp_variable.led14_push = 1;
        }
        else if (btn_key_val == 2) {
            no_storage.led_temp_variable.led15_push = 1;
        }
        else if (btn_key_val == 3) {
            no_storage.led_temp_variable.led16_push = 1;
        }
    }
    else if (user_date_all->part1 == type_x_part4 &&
        user_date_all->part2 == type_x_part1 &&
        user_date_all->part3 == type_null_x_part &&
        user_date_all->part4 == type_x_part1) // type_1_2_slide and double type_1_4
    {
        if (btn_key_val == 0) {
            no_storage.led_temp_variable.led13_push = 1;
        }
        else if (btn_key_val == 1) {
            no_storage.led_temp_variable.led14_push = 1;
        }
        else if (btn_key_val == 2) {
            no_storage.led_temp_variable.led15_push = 1;
        }
        else if (btn_key_val == 3) {
            no_storage.led_temp_variable.led16_push = 1;
        }
        else if (btn_key_val == 4) {
            no_storage.led_temp_variable.led1_push = 1;
        }
        else if (btn_key_val == 5) {
            no_storage.led_temp_variable.led2_push = 1;
        }
        else if (btn_key_val == 6) {
            no_storage.led_temp_variable.led3_push = 1;
        }
        else if (btn_key_val == 7) {
            no_storage.led_temp_variable.led4_push = 1;
        }
    }
    else {
        xlog_tag_message(TAG, "correction_real_lednum no type\n");
    }
}
static uint8_t tempbuff[10] = { 0 };
static uint8_t reset_time = 0, last_keynum = 0xff, reset_flag = 0;
static uint16_t value_t = 1000;
extern uint8_t count_;
void button_callback(void* button) {
    static uint16_t capture_math_time = 0x00, reset_math_time = 0;
    volatile uint32_t btn_event_val = 0, abs = 0;
    static volatile uint32_t sys_tic = 0, sys_tic_old = 0, key1_sys_tic = 0, key1_sys_tic_old = 0;
    static volatile uint8_t btn_key_val_old = 0xff, btn_key_val = 0, Tcmd = 0;
    btn_event_val = get_button_event((struct Button*)button);
    btn_key_val = get_button_keyid((struct Button*)button);
    switch (btn_event_val) {
    case PRESS_DOWN:
        no_storage.temp_lianpai.Temp_local_mode = 1;
        no_storage.temp_lianpai.Temp_time_cnt = 0;
        count_ = 2;
        device_ioctl(dev_motor_t, IOCTL_GROUP_SET, &value_t);
        xlog_tag_info(TAG, "key %d , __get_ticks %d \n", btn_key_val, (uint32_t)__get_ticks());
        sys_tic = __get_ticks();
        abs = sys_tic > sys_tic_old ? sys_tic - sys_tic_old : sys_tic_old - sys_tic;
        if (abs < 100) {
            xlog_tag_info(TAG, "sys_tic_old %d, sys_tic is %d, abs is %d\n,", sys_tic_old, sys_tic, abs);
            return;
        }
        sys_tic_old = sys_tic;
        correction_real_lednum(btn_key_val);
        if (btn_key_val == 0)
        {
            if (reset_time == 0)
            {
                reset_time = 40;
            }
            else
            {
                reset_flag = 1;
            }
            last_keynum = btn_key_val;
        }
        else
        {
            last_keynum = 0xff; reset_flag = 0; reset_time = 0;
        }
        break;

    case PRESS_UP:
        sys_tic = 0;
        capture_math_time = 0x00;
        if (Tcmd == L_KEYDOWN || Tcmd == CAPTURE) {
            Tcmd = L_KEYUP;
            tempbuff[0] = btn_key_val + 1;
            tempbuff[1] = Tcmd;
            protocol_mobus_dev_report(MOBUS_FRAME_TYPE_COMMAND, 0, tempbuff, 2);
        }
        break;

    case PRESS_REPEAT:

        break;

    case SINGLE_CLICK:
        sys_tic = __get_ticks();
        abs = sys_tic > sys_tic_old ? sys_tic - sys_tic_old : sys_tic_old - sys_tic;
        if (abs < 100) {
            xlog_tag_info(TAG, "sys_tic_old %d, sys_tic is %d, abs is %d\n,",
                sys_tic_old, sys_tic, abs);
            return;
        }
        sys_tic_old = sys_tic;
        Tcmd = S_CLICK;
        tempbuff[0] = btn_key_val + 1;
        tempbuff[1] = Tcmd;
        protocol_mobus_dev_report(MOBUS_FRAME_TYPE_COMMAND, 0, tempbuff, 2);
        break;

    case DOUBLE_CLICK:
        last_keynum = 0xff;
        Tcmd = D_CLICK;
        tempbuff[0] = btn_key_val + 1;
        tempbuff[1] = Tcmd;
        protocol_mobus_dev_report(MOBUS_FRAME_TYPE_COMMAND, 0, tempbuff, 2);
        xlog_tag_info(TAG, "button _num is %d _sta is DOUBLE_CLICK\n",
            btn_key_val + 1);
        break;

    case LONG_PRESS_START:
        Tcmd = L_KEYDOWN;
        tempbuff[0] = btn_key_val + 1;
        tempbuff[1] = Tcmd;
        protocol_mobus_dev_report(MOBUS_FRAME_TYPE_COMMAND, 0, tempbuff, 2);
        break;

    case LONG_PRESS_HOLD:
        if (reset_flag == 1)
        {
            reset_math_time++;
            if (reset_math_time > 1000)
                reset_math_time = 1000;
            else if (reset_math_time > 200)
            {
                cpu_reset();
            }
        }
        capture_math_time++;
        if (capture_math_time > 1000)
            capture_math_time = 1000;
        else if (capture_math_time > 400 && Tcmd != CAPTURE) {
            no_storage.led_temp_variable.dev_work_mode = LED_MODE_START;
            Tcmd = CAPTURE;
            tempbuff[0] = 0x10;
            tempbuff[1] = 0x01;
            tempbuff[2] = btn_key_val + 1;
            protocol_mobus_dev_report(MOBUS_FRAME_TYPE_CAPTURE, 0, tempbuff, 3);
        }
        break;
    }
}
static void multi_button_init(void) {
    bool err_flag = true;
    dev_motor_t = resources_get("dev_motor");
    assert(dev_motor_t);
    pdesc = ((device_t*)dev_key)->pdesc;
    assert(pdesc);
    no_storage.temp_key.key_num = pdesc->group_num_max;
    user_date_all = (struct struct_all_type*)pdesc->user_date;
    dev = (struct storage_mobus_device*)storage_get(STORAGE_AREA_DEVICE);
    assert(dev);
    lianpai_data = (struct  storage_lianpai_struct*)storage_get(STORAGE_AREA_LIANPAN);
    assert(lianpai_data);
    dev->device.device_type = MOBUS_DEVICE_TYPE_RUEN;
    if (user_date_all->part1 == type_x_part1 &&
        user_date_all->part2 == type_x_part1 &&
        user_date_all->part3 == type_x_part1 &&
        user_date_all->part4 == type_x_part1) // four type_1_4
    {
        button_init(&button[0], read_button1_gpio, 0, 0);
        button_init(&button[1], read_button2_gpio, 0, 1);
        button_init(&button[2], read_button3_gpio, 0, 2);
        button_init(&button[3], read_button4_gpio, 0, 3);
        button_init(&button[8], read_button9_gpio, 0, 4);
        button_init(&button[9], read_button10_gpio, 0, 5);
        button_init(&button[10], read_button11_gpio, 0, 6);
        button_init(&button[11], read_button12_gpio, 0, 7);
        button_init(&button[4], read_button5_gpio, 0, 8);
        button_init(&button[5], read_button6_gpio, 0, 9);
        button_init(&button[6], read_button7_gpio, 0, 10);
        button_init(&button[7], read_button8_gpio, 0, 11);
        button_init(&button[12], read_button13_gpio, 0, 12);
        button_init(&button[13], read_button14_gpio, 0, 13);
        button_init(&button[14], read_button15_gpio, 0, 14);
        button_init(&button[15], read_button16_gpio, 0, 15);
        err_flag = false;
    }
    else if (user_date_all->part1 == type_x_part2 &&
        user_date_all->part2 == type_x_part2 &&
        user_date_all->part3 == type_null_x_part &&
        user_date_all->part4 == type_null_x_part) // double type_1_2_y
    {
        button_init(&button[0], read_button1_gpio, 0, 0);
        button_init(&button[1], read_button2_gpio, 0, 1);
        button_init(&button[2], read_button3_gpio, 0, 2);
        button_init(&button[3], read_button4_gpio, 0, 3);
        button_init(&button[4], read_button5_gpio, 0, 4);
        button_init(&button[5], read_button6_gpio, 0, 5);
        button_init(&button[6], read_button7_gpio, 0, 6);
        button_init(&button[7], read_button8_gpio, 0, 7);
        err_flag = false;
    }
    else if (user_date_all->part1 == type_x_part1 &&
        user_date_all->part2 == type_x_part1 &&
        user_date_all->part3 == type_x_part3 &&
        user_date_all->part4 == type_null_x_part) // double type_1_4 and type_1_2_x
    {
        button_init(&button[0], read_button1_gpio, 0, 0);
        button_init(&button[1], read_button2_gpio, 0, 1);
        button_init(&button[2], read_button3_gpio, 0, 2);
        button_init(&button[3], read_button4_gpio, 0, 3);
        button_init(&button[8], read_button9_gpio, 0, 4);
        button_init(&button[9], read_button10_gpio, 0, 5);
        button_init(&button[10], read_button11_gpio, 0, 6);
        button_init(&button[11], read_button12_gpio, 0, 7);
        button_init(&button[4], read_button5_gpio, 0, 8);
        button_init(&button[5], read_button6_gpio, 0, 9);
        button_init(&button[6], read_button7_gpio, 0, 10);
        button_init(&button[7], read_button8_gpio, 0, 11);
        err_flag = false;
    }
    else if (user_date_all->part1 == type_x_part4 &&
        user_date_all->part2 == type_x_part5 &&
        user_date_all->part3 == type_null_x_part &&
        user_date_all->part4 == type_null_x_part) // type_1_2_slide and type_1_2_sw
    {
        button_init(&button[0], read_button5_gpio, 0, 0);
        button_init(&button[1], read_button6_gpio, 0, 1);
        err_flag = false;
        dev->device.device_type = MOBUS_DEVICE_TYPE_RUEN_WITH_SLIDER;
    }

    else if (user_date_all->part1 == type_x_part3 &&
        user_date_all->part2 == type_null_x_part &&
        user_date_all->part3 == type_x_part3 &&
        user_date_all->part4 == type_null_x_part) // double type_1_2_x
    {
        button_init(&button[0], read_button1_gpio, 0, 0);
        button_init(&button[1], read_button2_gpio, 0, 1);
        button_init(&button[2], read_button3_gpio, 0, 2);
        button_init(&button[3], read_button4_gpio, 0, 3);
        button_init(&button[4], read_button9_gpio, 0, 4);
        button_init(&button[5], read_button10_gpio, 0, 5);
        button_init(&button[6], read_button11_gpio, 0, 6);
        button_init(&button[7], read_button12_gpio, 0, 7);
        err_flag = false;
    }
    /****       wait add
    else if (user_date_all->part1 == type_x_part2 &&
        user_date_all->part2 == type_x_part1 &&
        user_date_all->part3 == type_null_x_part &&
        user_date_all->part4 == type_x_part1) // type_1_2_y add double type_1_4
    {
        button_init(&button[0], read_button1_gpio, 0, 0);
        button_init(&button[1], read_button2_gpio, 0, 1);
        button_init(&button[2], read_button3_gpio, 0, 2);
        button_init(&button[3], read_button4_gpio, 0, 3);
        button_init(&button[4], read_button5_gpio, 0, 4);
        button_init(&button[5], read_button6_gpio, 0, 5);
        button_init(&button[6], read_button7_gpio, 0, 6);
        button_init(&button[7], read_button8_gpio, 0, 7);
        button_init(&button[8], read_button13_gpio, 0, 8);
        button_init(&button[9], read_button14_gpio, 0, 9);
        button_init(&button[10], read_button15_gpio, 0, 10);
        button_init(&button[11], read_button16_gpio, 0, 11);
        err_flag = false;
    }
    else if (user_date_all->part1 == type_x_part1 &&
        user_date_all->part2 == type_x_part2 &&
        user_date_all->part3 == type_x_part1 &&
        user_date_all->part4 == type_null_x_part) // double type_1_4 add  type_1_2_y
    {
        button_init(&button[0], read_button1_gpio, 0, 0);
        button_init(&button[1], read_button2_gpio, 0, 1);
        button_init(&button[2], read_button3_gpio, 0, 2);
        button_init(&button[3], read_button4_gpio, 0, 3);
        button_init(&button[4], read_button9_gpio, 0, 4);
        button_init(&button[5], read_button10_gpio, 0, 5);
        button_init(&button[6], read_button11_gpio, 0, 6);
        button_init(&button[7], read_button12_gpio, 0, 7);
        button_init(&button[8], read_button5_gpio, 0, 8);
        button_init(&button[9], read_button6_gpio, 0, 9);
        button_init(&button[10], read_button7_gpio, 0, 10);
        button_init(&button[11], read_button8_gpio, 0, 11);
        err_flag = false;
    }
    else if (user_date_all->part1 == type_x_part4 &&
        user_date_all->part2 == type_x_part1 &&
        user_date_all->part3 == type_null_x_part &&
        user_date_all->part4 ==
        type_x_part1) // type_1_2_slide and double type_1_4
    {
        button_init(&button[0], read_button5_gpio, 0, 0);
        button_init(&button[1], read_button6_gpio, 0, 1);
        button_init(&button[2], read_button7_gpio, 0, 2);
        button_init(&button[3], read_button8_gpio, 0, 3);
        button_init(&button[4], read_button13_gpio, 0, 4);
        button_init(&button[5], read_button14_gpio, 0, 5);
        button_init(&button[6], read_button15_gpio, 0, 6);
        button_init(&button[7], read_button16_gpio, 0, 7);
        err_flag = false;
        dev->device.device_type = MOBUS_DEVICE_TYPE_RUEN_WITH_SLIDER;
    }
    else if (user_date_all->part1 == type_x_part4 &&
        user_date_all->part2 == type_x_part2 &&
        user_date_all->part3 == type_null_x_part &&
        user_date_all->part4 ==
        type_null_x_part) // type_1_2_slide and type_1_2_y
    {
        button_init(&button[0], read_button5_gpio, 0, 0);
        button_init(&button[1], read_button6_gpio, 0, 1);
        button_init(&button[2], read_button7_gpio, 0, 2);
        button_init(&button[3], read_button8_gpio, 0, 3);
        err_flag = false;
        dev->device.device_type = MOBUS_DEVICE_TYPE_RUEN_WITH_SLIDER;
    }
   *****/
    if (err_flag == false) {
        storage_set(STORAGE_AREA_DEVICE, dev, sizeof(struct storage_mobus_device));
        for (unsigned char i = 0; i < pdesc->group_num_max; i++) {
            button_attach(&button[i], PRESS_DOWN, button_callback);
            button_attach(&button[i], PRESS_UP, button_callback);
            button_attach(&button[i], DOUBLE_CLICK, button_callback);
            button_attach(&button[i], SINGLE_CLICK, button_callback);
            button_attach(&button[i], LONG_PRESS_START, button_callback);
            button_attach(&button[i], LONG_PRESS_HOLD, button_callback);
            button_start(&button[i]);
        }
    }
    else {
        xlog_tag_error(TAG, "button_init is failed\n");
    }
}

static void _timer_expired(timer_handle_t timer) {
    if (no_storage.led_temp_variable.dev_work_mode != LED_MODE_WORK)
        return;
    button_ticks();
    if (reset_time > 0)
        reset_time--;
    else
        last_keynum = 0xff;
    if (user_date_all->part1 == type_x_part4) {
        volatile uint16_t* real_adc_g = NULL;
        volatile uint16_t real_adc_temp = 0;
        static volatile uint8_t report_slider_dly = 0, adc_check_flag = 0, send_once_100ms;
        static uint8_t ledx_pwm[1] = { 0 };
        // void *htr321x = NULL;
        // htr321x = resources_get("dev_htr3218");
        // assert(htr321x);
        real_adc_g = (uint16_t*)pdesc->user_date_special;
        for (unsigned char i = 0; i < 10; i++) {
            real_adc_group[i] = *(real_adc_g++);
            real_adc_temp += real_adc_group[i];
        }
        real_adc_old = (float)(real_adc_temp / 10);
        if ((real_adc_old > real_adc ? real_adc_old - real_adc
            : real_adc - real_adc_old) > 40) {
            if (real_adc_old != 0 && real_adc == 0) {
                real_adc = real_adc_old;
                return;
            }
            no_storage.led_temp_variable.led5_push = 1;
            real_adc = real_adc_old;
            ledx_pwm[0] = (uint8_t)((float)(100 * (real_adc - MIN_SLIDER) / (MAX_SLIDER - MIN_SLIDER))); // no negation
            if (ledx_pwm[0] <= 1)
                ledx_pwm[0] = 0;
            if (ledx_pwm[0] >= 99)
                ledx_pwm[0] = 100;  // device_write(htr321x,&ledx_pwm,HTR321x_LED4,1);
                                    // device_ioctl(htr321x,IOCTL_HTR321x_UPDATA,NULL);
            send_once_100ms++;
            report_slider_dly = 80;
            if (no_storage.temp_lianpai.Temp_local_mode == 1)
            {
                if (send_once_100ms % 20 == 1) {
                    adc_check_flag = 1;
                    if (tempbuff[0] == 0)
                        tempbuff[0] = 1;
                    tempbuff[1] = SLIDER;
                    tempbuff[2] = ledx_pwm[0];
                    protocol_mobus_dev_report(MOBUS_FRAME_TYPE_COMMAND, 0, tempbuff, 3);
                    xlog_info("seng by 100 ms %d", send_once_100ms);
                }
            }
            else
            {
                no_storage.temp_lianpai.Temp_time_cnt = no_storage.temp_lianpai.lianpai_Time;
            }
        }
        else {
            if (report_slider_dly > 0)
                report_slider_dly--;
            if ((adc_check_flag == 1 && report_slider_dly == 0) ||
                report_slider_dly == 1) {
                adc_check_flag = 0;
                if (no_storage.temp_lianpai.Temp_local_mode == 1)
                {
                    if (tempbuff[0] == 0)
                        tempbuff[0] = 1;
                    tempbuff[1] = SLIDER;
                    tempbuff[2] = ledx_pwm[0];
                    protocol_mobus_dev_report(MOBUS_FRAME_TYPE_COMMAND, 0, tempbuff, 3);
                }
                else if (no_storage.temp_lianpai.Temp_local_mode == 2)
                {
                    tempbuff[0] = 0x03;
                    tempbuff[1] = no_storage.temp_lianpai.Temp_key_adress;
                    tempbuff[2] = no_storage.temp_lianpai.Temp_key_channl;
                    tempbuff[3] = no_storage.temp_lianpai.Temp_key_num;
                    tempbuff[4] = LPSLIDER;
                    tempbuff[5] = ledx_pwm[0];
                    protocol_mobus_Dev_forwarding_protocol(MOBUS_FRAME_TYPE_DEV_FORWARD, 0, tempbuff, 6);
                }
            }
        }
        flag_1S++;
        if (flag_1S > 100)
        {
            flag_1S = 0;
            if (no_storage.temp_lianpai.Temp_local_mode == 2)
            {
                if (no_storage.temp_lianpai.Temp_time_cnt > 0)
                {
                    no_storage.temp_lianpai.Temp_time_cnt--;
                }
                else
                {
                    no_storage.temp_lianpai.Temp_local_mode = 1;
                    xlog_tag_message(TAG, "Temp_local_mode is %d\r\n", no_storage.temp_lianpai.Temp_local_mode);
                }
            }
        }
    }
}

bool app_key_poll(void) {
    bool _init_flag = false;
    dev_key = resources_get("dev_key");
    assert(dev_key);
    multi_button_init();
    /* create a timer for proc polling */
    proc_timer = soft_timer_create("KeyProcTimer", SFTIM_MODE_REPEAT,
        __ms2ticks(10), NULL, _timer_expired);
    assert(proc_timer);
    soft_timer_start(proc_timer);
    /*say hi */
    xlog_tag_message(TAG, "Initialize successfully\n");
    _init_flag = true;
    return _init_flag;
}
